﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClientRegister
{
    public partial class ChangeClassInfoForm : Form
    {

        public string IdClass { set; get; }
        public string NameClass { set; get; }
        public string IdSub { set; get; }
        public string IdTeacher { set; get; }
        public string Studyday { set; get; }
        public string Room { set; get; }
        public string Time { set; get; }
        public string Fee { set; get; }
        public string Start { set; get; }
        public string End { set; get; }
        public string accept { set; get; }
        public string note { set; get; }
        public string CreateDate { set; get; }

        public string IDEmp { set; get; }
        public string NameEmp { set; get; }

        SynchronizeData sync = new SynchronizeData();

        public ChangeClassInfoForm()
        {
            InitializeComponent();
        }

        private void ChangeClassInfoForm_Load(object sender, EventArgs e)
        {
            ////Nhan du lieu tu server
            //sync.GetDataForInsertMethodWithID(sync.ClassTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.ClassTb, sync.updatecol);

            txtID.Text = IdClass;
            txtName.Text = NameClass;
            txtIDSub.Text = IdSub;
            txtIDTeacher.Text = IdTeacher;
            txtStudyday.Text = Studyday;
            txtRoom.Text = Room;
            txtTime.Text = Time;
            txtFee.Text = Fee;
            txtStart.Text = DateTime.Parse(Start).ToShortDateString();
            txtEnd.Text = DateTime.Parse(End).ToShortDateString();
            if (int.Parse(accept) == 1) chkAccept.Checked = true;
            else chkAccept.Checked = false;
           // txtNote.Text = note;
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (txtNote.Text == "")
                MessageBox.Show(" Cần phải ghi chú thông tin chỉnh sửa");
            else
            {
                string noteC = note;
                noteC += "- Chinh sua ngay " + DateTime.Now.ToString() + " Boi nhan vien: ID: " +IDEmp+", Ten NV: "+NameEmp+ "\n";
                noteC += "- Noi dung: "+ txtNote.Text + "\n";

                if (txtName.Text != NameClass)
                    noteC += "- " + NameClass + "\n";

                if (txtIDTeacher.Text != IdTeacher)
                    noteC += "- " + IdTeacher + "\n";

                if (txtStudyday.Text != Studyday)
                    noteC += "- " + Studyday + "\n";

                if (txtRoom.Text != Room)
                    noteC += "- " + Room + "\n";

                if (txtTime.Text != Time)
                    noteC += "- " + Time + "\n";

                if (txtFee.Text != Fee)
                    noteC += "- " + Fee + "\n";

                if (txtStart.Text != Start)
                    noteC += "- " + Start + "\n";

                if (txtEnd.Text != End)
                    noteC += "- " + End + "\n";

                int regis = 0;
                if (chkAccept.Checked == true) regis = 1;
                else regis = 0;

                if (int.Parse(accept) != regis)
                    noteC += "- " + accept + "\n";

                ClassDataProvider change = new ClassDataProvider();
                int result = change.ChangeInfoClass(IdClass, txtName.Text, txtIDTeacher.Text, txtStudyday.Text, txtRoom.Text,
                                                    txtTime.Text, txtFee.Text, txtStart.Text, txtEnd.Text, regis.ToString(), noteC);
                if (result == 1)
                {
                    //string xml = change.ConvertClassToXML(IdClass);
                    ////Chuyen du lieu len server
                    //sync.transfDataForUpdateMethod(xml, sync.ClassTb);

                    MessageBox.Show("Thay đổi thông tin thành công");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gbChooseTeacher_Enter(object sender, EventArgs e)
        {
            TeacherDataProvider choose = new TeacherDataProvider();
            DataSet ds = choose.LoadTeacher(IdSub);
            DataTable dt = ds.Tables[0];
            cbbTeacher.DataSource = dt;
            cbbTeacher.DisplayMember = "FullName";
            cbbTeacher.ValueMember = "ID";
        }

        private void gbChooseStudyDay_Enter(object sender, EventArgs e)
        {

        }

        private void gbChooseRoom_Enter(object sender, EventArgs e)
        {
            RoomDataProvider choose = new RoomDataProvider();
            DataSet ds = choose.LoadRoom();
            DataTable dt = ds.Tables[0];
            cbbRoom.DataSource = dt;
            cbbRoom.DisplayMember = "RoomName";
            cbbRoom.ValueMember = "RoomName";
        }

        private void gbChooseTime_Enter(object sender, EventArgs e)
        {

        }

        private void linklblChangeTeacher_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            gbChooseTeacher.Show();
        }

        private void linklblChangeStudyday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            gbChooseStudyDay.Show();
        }

        private void linklblChangeRoom_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            gbChooseRoom.Show();
        }

        private void linklblChangeTime_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            gbChooseTime.Show();
        }

        private void cbbTeacher_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbTeacher.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                string idteacher = cbbTeacher.SelectedValue.ToString();
                if(idteacher != txtIDTeacher.Text)
                    txtIDTeacher.Text = idteacher;
            }
        }

        private void cbbStudyDay_SelectedIndexChanged(object sender, EventArgs e)
        {
            string stu = cbbStudyDay.SelectedItem.ToString();
            if (stu != txtStudyday.Text)
                txtStudyday.Text = stu;
        }

        private void cbbRoom_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cbbRoom.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                string room = cbbRoom.SelectedValue.ToString();
                if (room != txtRoom.Text)
                    txtRoom.Text = room;
            }
        }

        private void cbbTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            string time = cbbTime.SelectedItem.ToString();
            if (time != txtTime.Text)
                txtTime.Text = time;
        }
    }
}
